//
//  ZGLRecordDetailController.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/7/3.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLRecordDetailController.h"
#import "ZGLLotteryRecordController.h"
#import "ZGLOrderPayController.h"
#import <WebKit/WebKit.h>
#import "ZGLMainViewController.h"

//@class ZGLLotteryRecordController;
@interface ZGLRecordDetailController ()
<WKUIDelegate,
WKNavigationDelegate,
WKScriptMessageHandler,
UIGestureRecognizerDelegate>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, assign) BOOL isLast;

@end

@implementation ZGLRecordDetailController

- (WKWebView *)webView
{
    if (!_webView) {
        //创建网页配置对象
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        
        //点击按钮
        [config.userContentController addScriptMessageHandler:self name:@"ContinueToPlay"];
        [config.userContentController addScriptMessageHandler:self name:@"ImmediatePay"];
        
        // 创建设置对象
        WKPreferences *preference = [[WKPreferences alloc]init];
        //最小字体大小 当将javaScriptEnabled属性设置为NO时，可以看到明显的效果
        preference.minimumFontSize = 0;
        //设置是否支持javaScript 默认是支持的
        preference.javaScriptEnabled = YES;
        // 在iOS上默认为NO，表示是否允许不经过用户交互由javaScript自动打开窗口
        preference.javaScriptCanOpenWindowsAutomatically = YES;
        config.preferences = preference;
        
        // 是使用h5的视频播放器在线播放, 还是使用原生播放器全屏播放
        config.allowsInlineMediaPlayback = YES;
        //设置视频是否需要用户手动播放  设置为NO则会允许自动播放
        config.requiresUserActionForMediaPlayback = YES;
        //设置是否允许画中画技术 在特定设备上有效
        config.allowsPictureInPictureMediaPlayback = YES;
        //设置请求的User-Agent信息中应用程序名称 iOS9后可用
        config.applicationNameForUserAgent = @"ZeegingLottery";
        
        _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT-STATUEHEIGHT) configuration:config];
        [_webView.scrollView setShowsVerticalScrollIndicator:NO];
        // UI代理
        _webView.UIDelegate = self;
        // 导航代理
        _webView.navigationDelegate = self;
        
        NSURL *url = [NSURL URLWithString:self.urlString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];
    }
    
    
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"订单详情";
    self.view.backgroundColor = ViewBackgroundColor;
    
    [self.view addSubview:self.webView];
    
//    if ([self isFoundOutLastOneController:[ZGLSuccessCallBackVC class]]) {
//        self.navigationController.interactivePopGestureRecognizer.delegate = self;
//    }
    
//    if (self.isTransfer) {
//        [self setLeftBackButtonWith:nil Image:nil];
//    }

}

//- (void)goBack
//{
////    [self.navigationController popToRootViewControllerAnimated:YES];
////    [self.navigationController popoverPresentationController];
//
//    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
//}

//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer
//                                      *)gestureRecognizer{
//
//    if (self.isLast) {
//        return NO;
//    }else{
//        return YES;
//    }
////    return NO; //YES：允许右滑返回  NO：禁止右滑返回
//}
//
////判断上一个vc
//- (BOOL )isFoundOutLastOneController:(Class )class
//{
//    NSArray *array = self.navigationController.viewControllers;
////    NSInteger VcCount = array.count;
////    UIViewController *lastVc = array[VcCount-1];
//
//    self.isLast = false;
//    if (array) {
//
//        for (UIViewController *Item in array) {
//            if (Item && [Item isKindOfClass:class]) {
//                 return _isLast = true;
//            }else{
//                _isLast = false;
//            }
//        }
//    }
//
//    return _isLast;
//}

#pragma mark - method

//- (void)goBack
//{
//
////    NSArray *array = self.navigationController.viewControllers;
//
//    __block BOOL isPop = NO;
//
//    [self.navigationController.viewControllers enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//
//        NSString *vcName = NSStringFromClass([obj class]);
//        NSLog(@"控制器名称: %@ index=%ld", vcName, idx);
//
//        if ([vcName isEqualToString:NSStringFromClass([ZGLSuccessCallBackVC class])]) {
//
////            ZGLMainViewController *mainVC = [[ZGLMainViewController alloc]init];
////            [self.navigationController popToViewController:mainVC animated:YES];
//
//            [self.navigationController popToRootViewControllerAnimated:NO];
//            isPop = YES;
//            *stop = YES;
//        }
//    }];
//
//    if (!isPop) {
//
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//
//}


- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    DLog(@"name:%@\\\\n body:%@\\\\n frameInfo:%@\\\\n",message.name,message.body,message.frameInfo);
    NSDictionary * parameter = message.body;
    //JS调OC
    if ([message.name isEqualToString:@"ContinueToPlay"]) {
        
        //
        DLog(@"继续玩竞彩足球");
        
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }else if ([message.name isEqualToString:@"ImmediatePay"]) {
        
        
//        NSString *order_no = parameter[@"order_no"];
//        NSString *amount = parameter[@"amount"];
        DLog(@"立即支付");
        
        ZGLOrderPayController *orderPayVC = [[ZGLOrderPayController alloc]init];
        orderPayVC.orderInfoDict = parameter;
        [self.navigationController pushViewController:orderPayVC animated:YES];
        
    }
    
    
    
    
}


- (void)dealloc
{
    DLog(@"webView释放");
    [[_webView configuration].userContentController removeAllUserScripts];
    
}

@end
